#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WSite 应用工厂方法
script name: __init__.py
Created on 2020/2/15 17:42 by PyCharm
@author: fujianjian
"""
from concurrent.futures.thread import ThreadPoolExecutor
from queue import Queue

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_cors import CORS
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy

from .utils.serializable import JSONEncoder
from config import config

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()


def create_app(config_naem):
    wsite = Flask(__name__)
    # 重写flask默认饿JSON序列化
    wsite.json_encoder = JSONEncoder
    active_config = config[config_naem]
    wsite.config.from_object(active_config)
    active_config.init_app()

    bootstrap.init_app(app=wsite)
    mail.init_app(app=wsite)
    moment.init_app(app=wsite)
    db.init_app(app=wsite)

    # 支持跨域请求
    CORS(app=wsite, supports_credentials=True)

    # 指定全局的错误页面
    # .....
    from .actress import actress_blueprint
    wsite.register_blueprint(actress_blueprint, url_prefix='/actress')
    from .seed import seed_blueprint
    wsite.register_blueprint(seed_blueprint, url_prefix='/seed')
    from .vehicle import vehicle_blueprint
    wsite.register_blueprint(vehicle_blueprint, url_prefix='/vehicle')
    from .auth import auth_blueprint
    wsite.register_blueprint(auth_blueprint, url_prefix='/auth')
    from .plan import plan_blueprint
    wsite.register_blueprint(plan_blueprint, url_prefix='/plan')

    return wsite


DEFAULT_PAGE_SIZE = 20
DEFAULT_PAGE_NO = 1

# Queue 为FIFO队列, maxsize指定队列长度，小于0 代表无限长度
find_seed_queue = Queue(maxsize=0)

wsite_thread_pool = ThreadPoolExecutor(max_workers=10, thread_name_prefix='wsite-thread-pool')
